#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import multiprocessing
import time


def myprint(obj):
    sys.stdout.write(str(obj) + '\n')


def wait_for_event(e):
    """Wait for the event to be set before doing anything"""

    myprint('%s:wait_for_event: starting' % time.ctime())
    e.wait()
    myprint('%s:wait_for_event: e.is_set()-> %s' % (time.ctime(),e.is_set()))


def wait_for_event_timeout(e, t):
    """Wait t seconds and then timeout"""

    myprint('%s:wait_for_event_timeout: starting' % time.ctime())
    e.wait(t)
    myprint('%s:wait_for_event_timeout: e.is_set()-> %s' % (time.ctime(),e.is_set()))


if __name__ == '__main__':
    e = multiprocessing.Event()
    w1 = multiprocessing.Process(name='block', target=wait_for_event,
                                 args=(e, ))
    w1.start()

    w2 = multiprocessing.Process(name='non-block',
                                 target=wait_for_event_timeout,
                                 args=(e, 2))
    w2.start()

    myprint('%s:main: waiting before calling Event.set()' % time.ctime())
    time.sleep(3)
    e.set()
    myprint('%s:main: event is set' % time.ctime())
